package com.sky.trade.controller;

import com.sky.common.exception.OrderBusinessException;
import com.sky.common.result.PageResult;
import com.sky.common.result.Result;

import com.sky.trade.domain.dto.OrdersPaymentDTO;
import com.sky.trade.domain.dto.OrdersSubmitDTO;
import com.sky.trade.domain.vo.OrderPaymentVO;
import com.sky.trade.domain.vo.OrderSubmitVO;
import com.sky.trade.domain.vo.OrderVO;
import com.sky.trade.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "用户-订单相关接口")
@Slf4j
public class UserOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     *
     * @param ordersSubmitDTO 订单信息
     * @return 订单提交结果
     */
    @PostMapping("/submit")
    @ApiOperation("提交订单")
    public Result<OrderSubmitVO> Submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户提交订单：{}", ordersSubmitDTO);
        try {
            OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
            return Result.success(orderSubmitVO);
        } catch (OrderBusinessException e) {
            // 捕获超出配送范围的异常并返回友好的提示
            if ("超出配送范围".equals(e.getMessage())) {
                return Result.error("抱歉，您的配送地址超出了我们的配送范围，请查看可配送区域。");
            }
            // 处理其他可能的业务异常
            return Result.error("提交订单时发生错误，请稍后再试。");
        }
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 历史订单查询
     *
     * @param page     分页数
     * @param pageSize 每页条数
     * @param status   订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     * @return 历史订单分页信息
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> page(int page, int pageSize, Integer status) {
        log.info("查询用户历史订单，page：{},pageSize：{},status：{}", page, pageSize, status);
        PageResult pageResult = orderService.pageQueryUser(page, pageSize, status);
        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     *
     * @param id 订单id
     * @return 订单详情
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable Long id) {
        log.info("查询订单详情，id：{}", id);
        OrderVO orderVO = orderService.details(id);
        return Result.success(orderVO);
    }

    /**
     * 取消订单
     *
     * @param id 订单id
     * @return 取消结果
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable Long id) throws Exception  {
        log.info("取消订单，id：{}", id);
        orderService.userCancelById(id);
        return Result.success();
    }

    /**
     * 再来一单
     *
     * @param id 订单id
     * @return 再来一单结果
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result repetition(@PathVariable Long id) {
        log.info("再来一单: {}", id);
        orderService.repetition(id);
        return Result.success();
    }

    @GetMapping("/reminder/{id}")
    @ApiOperation("催单")
    public Result reminder(@PathVariable Long id){
        log.info("催单: {}", id);
        orderService.reminder(id);
        return Result.success();
    }
}
